#ifndef GameMessageQueue_h__
#define GameMessageQueue_h__

#include "GameMessage.h"
#include "Mutex.h"
#include <set>

typedef std::set<GameMessage*,GameMessageCompare> sMessages;
class GameMessageQueue
{
private:
    sMessages m_set;
    CriticalSection m_mutex;

public:
    void push(GameMessage* data)
    {
        ScopedCriticalSection l(m_mutex);
        m_set.insert(data);
    }

    bool empty()
    {
        ScopedCriticalSection l(m_mutex);
        return m_set.empty();
    }

    GameMessage* try_pop(bool* worked)
    {
        ScopedCriticalSection l(m_mutex);
        if(m_set.empty())
        {
            *worked = false;
            return NULL;
        }
        *worked = true;
        GameMessage* re = (*m_set.begin());
        m_set.erase(m_set.begin());
        return re;
    }

    void clean()
    {
        ScopedCriticalSection l(m_mutex);
        sMessages::iterator it = m_set.begin();
        for(; it != m_set.end(); ++it)
        {
            delete (*it);
        }
        m_set.clear();
    }
};


#endif // MessageQueue_h__
